export default class ScrollHeader {
    constructor(element, option) {
        this.$element = $(element);
        this.$window = $(window);
        this.timer = null;
        this.elementOffsetTop = this.$element.offset().top;
        this.$elementPlaceholder = this.$element.next();
        this.init();
    }

    init() {
        this.scrollHeaderFn();
        this.watchScroll();
    }


    fixed() {
        this.$element.addClass('fixed');
        this.$elementPlaceholder.show();
    }

    noFixed() {
        this.$element.removeClass('fixed');
        this.$elementPlaceholder.hide()
    }

    toggleFixed(isFixed) {
        return isFixed ? this.fixed() : this.noFixed();
    }

    watchScroll() {
        this.$window.on('scroll.header', (e)=>{
            this.scrollHeaderFn(e)
        })
    }

    scrollHeaderFn(e) {
        this.timer && clearTimeout(this.timer);
        this.timer = setTimeout(() => {
            let scrollTop, elementOffsetTop;
            scrollTop = this.$window.scrollTop();
            elementOffsetTop = this.elementOffsetTop;
            this.toggleFixed(scrollTop >= elementOffsetTop)
        }, 13)
    }
}